<?php
class Pauler_Emailpdf_Model_Observer extends Mage_Sales_Model_Order{
    
    public function emailPdf($observer){
        $order = $observer->getEvent()->getOrder();
       /* $quote = $observer->getEvent()->getQuote();
        $orderId = $order->getId();
        $items = $order->getAllItems();//订单产品
        $customerId = $order->getCustomerId();//订单用户
        $orderId = $order->getIncrementId();//订单ID 有订单的时候和上面$orderId相等? */
        $this->sendAdminOrderEmail($order); 
    }
    /**
     * Send email with order data
     *
     * @return Mage_Sales_Model_Order
     */
    public function sendAdminOrderEmail15($order)
    {
        $storeId = $order->getStore()->getId();  
        // Start store emulation process
        $appEmulation = Mage::getSingleton('core/app_emulation');
        $initialEnvironmentInfo = $appEmulation->startEnvironmentEmulation($storeId);

        try {
            // Retrieve specified view block from appropriate design package (depends on emulated store)
            $paymentBlock = Mage::helper('payment')->getInfoBlock($order->getPayment())
                ->setIsSecureMode(true);
            $paymentBlock->getMethod()->setStore($storeId);
            $paymentBlockHtml = $paymentBlock->toHtml();
        } catch (Exception $exception) {
            // Stop store emulation process
            $appEmulation->stopEnvironmentEmulation($initialEnvironmentInfo);
            throw $exception;
        }

        // Stop store emulation process
        $appEmulation->stopEnvironmentEmulation($initialEnvironmentInfo);

        // Retrieve corresponding email template id and customer name
        if ($order->getCustomerIsGuest()) {
            $templateId = Mage::getStoreConfig(self::XML_PATH_EMAIL_GUEST_TEMPLATE, $storeId);
            $customerName = $order->getBillingAddress()->getName();
        } else {
            $templateId = Mage::getStoreConfig(self::XML_PATH_EMAIL_TEMPLATE, $storeId);
            $customerName = $order->getCustomerName();
        }

        $mailer = Mage::getModel('emailpdf/email_template_mailer');
        $emailInfo = Mage::getModel('core/email_info');
        $trans_email_ident_sales_email;
        $indent = Mage::getStoreConfig(self::XML_PATH_EMAIL_IDENTITY, $storeId);
        $email = Mage::getStoreConfig('trans_email/ident_'.$indent.'/email',$storeId);
        $name =  Mage::getStoreConfig('trans_email/ident_'.$indent.'/name',$storeId);
        //Mage::log($email);
        $emailInfo->addTo($email,$name);//发给管理员      
        $mailer->addEmailInfo($emailInfo); 
        // Set all required params and send emails
        $mailer->setSender(Mage::getStoreConfig(self::XML_PATH_EMAIL_IDENTITY, $storeId));
        $mailer->setStoreId($storeId);
        $mailer->setTemplateId($templateId);
        $mailer->setTemplateParams(array(
                'order'        => $order,
                'billing'      => $order->getBillingAddress(),
                'payment_html' => $paymentBlockHtml
            )
        );
        $mailer->send();

       // $order->setEmailSent(true);
        //$this->_getResource()->saveAttribute($order, 'email_sent');

        return $this;
    }
    /**
     * Sending email with order data
     *
     * @return Mage_Sales_Model_Order
     */
    public function sendAdminOrderEmail($order)
    {
        $storeId = $order->getStore()->getId();   
        $translate = Mage::getSingleton('core/translate');
        /* @var $translate Mage_Core_Model_Translate */
        $translate->setTranslateInline(false);

        $paymentBlock = Mage::helper('payment')->getInfoBlock($order->getPayment())
            ->setIsSecureMode(true);

        $paymentBlock->getMethod()->setStore($order->getStore()->getId());

        $mailTemplate = Mage::getModel('emailpdf/email_template');
       

        if ($this->getCustomerIsGuest()) {
            $template = Mage::getStoreConfig(self::XML_PATH_EMAIL_GUEST_TEMPLATE, $order->getStoreId());
            //$customerName = $order->getBillingAddress()->getName();
        } else {
            $template = Mage::getStoreConfig(self::XML_PATH_EMAIL_TEMPLATE, $order->getStoreId());
          //  $customerName = $order->getCustomerName();
        }
        $indent = Mage::getStoreConfig(self::XML_PATH_EMAIL_IDENTITY, $storeId);
        $email = Mage::getStoreConfig('trans_email/ident_'.$indent.'/email',$storeId);
        $name =  Mage::getStoreConfig('trans_email/ident_'.$indent.'/name',$storeId);
        $sendTo = array(
            array(
                'email' => $email,
                'name'  => $name
            )
        );
        

        foreach ($sendTo as $recipient) {
            $mailTemplate->setDesignConfig(array('area'=>'frontend', 'store'=>$order->getStoreId()))
                ->sendTransactional(
                    $template,
                    Mage::getStoreConfig(self::XML_PATH_EMAIL_IDENTITY, $order->getStoreId()),
                    $recipient['email'],
                    $recipient['name'],
                    array(
                        'order'         => $order,
                        'billing'       => $order->getBillingAddress(),
                        'payment_html'  => $paymentBlock->toHtml(),
                    )
                );
        }
       // $this->setEmailSent(true);
       // $this->_getResource()->saveAttribute($this, 'email_sent');
        $translate->setTranslateInline(true);

        return $this;
    }

    
}  
